package org.jeecgframework.web.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.service.CbsAppVersionServiceI;
import org.jeecgframework.web.system.pojo.base.CbsAppVersionEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



/**   
 * @Title: Controller
 * @Description: APP系统版本信息
 * @date 2017-03-01 17:07:42
 */
@Scope("prototype")
@Controller
@RequestMapping("/cbsAppVersionController")
public class CbsAppVersionController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CbsAppVersionController.class);

	@Autowired
	private CbsAppVersionServiceI cbsAppVersionService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * APP系统版本信息列表 页面跳转
	 */
	@RequestMapping(params = "cbsAppVersion")
	public ModelAndView cbsAppVersion(HttpServletRequest request) {
		return new ModelAndView("com/zybk/cbs_app_version/cbsAppVersionList");
	}

	/**
	 * easyui AJAX请求数据
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(CbsAppVersionEntity cbsAppVersion,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(CbsAppVersionEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, cbsAppVersion, request.getParameterMap());
		try{
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.cbsAppVersionService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * APP系统版本信息新增页面跳转
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(CbsAppVersionEntity cbsAppVersion, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(cbsAppVersion.getId())) {
			cbsAppVersion = cbsAppVersionService.getEntity(CbsAppVersionEntity.class, cbsAppVersion.getId());
			req.setAttribute("cbsAppVersionPage", cbsAppVersion);
		}
		return new ModelAndView("com/zybk/cbs_app_version/cbsAppVersion-add");
	}
	

	/**
	 * 添加APP系统版本信息
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(CbsAppVersionEntity cbsAppVersion, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "APP系统版本信息添加成功";
		try{
			cbsAppVersionService.save(cbsAppVersion);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "APP系统版本信息添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * APP系统版本信息编辑页面跳转
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(CbsAppVersionEntity cbsAppVersion, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(cbsAppVersion.getId())) {
			cbsAppVersion = cbsAppVersionService.getEntity(CbsAppVersionEntity.class, cbsAppVersion.getId());
			req.setAttribute("cbsAppVersionPage", cbsAppVersion);
		}
		return new ModelAndView("com/zybk/cbs_app_version/cbsAppVersion-update");
	}
	
	/**
	 * 更新APP系统版本信息
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(CbsAppVersionEntity cbsAppVersion, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "APP系统版本信息更新成功";
		CbsAppVersionEntity t = cbsAppVersionService.get(CbsAppVersionEntity.class, cbsAppVersion.getId());
		try {
			MyBeanUtils.copyBeanNotNull2Bean(cbsAppVersion, t);
			cbsAppVersionService.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "APP系统版本信息更新失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	

	
	/**
	 * 删除APP系统版本信息
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(CbsAppVersionEntity cbsAppVersion, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		cbsAppVersion = systemService.getEntity(CbsAppVersionEntity.class, cbsAppVersion.getId());
		message = "APP系统版本信息删除成功";
		try{
			cbsAppVersionService.delete(cbsAppVersion);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "APP系统版本信息删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * @author xiul
	 * 2016-10-28 下午3:09:17
	 * Description 查询APP最新版本号
	 */
	@ResponseBody
	@RequestMapping(params="querySysVersion")
	public AjaxJson querySysVersion(HttpServletRequest req){
		AjaxJson j = new AjaxJson();
		cbsAppVersionService.querySysVersion(req, j);
		return j;
	}
	
}
